using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WSTestApp.localhost;

namespace WSTestApp
{
    public partial class Form1 : Form
    {
        private Service s = null;

        public Form1()
        {
            InitializeComponent();

            s = new Service();
            s.Timeout = 1000000000;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label1.Text = "";

            s.CreateCoordinates(1000, 1000, 1000, 1000000);
            label1.Text = "Coordinates are created"; 
            s.CreateStarsIndex();
            label1.Text = "Index is created";
            GC.Collect();
            s.CreateStars();
            label1.Text = "Stars are created";

            GC.Collect();

            get.Enabled = true;
            addUsersBtn.Enabled = true;

            //label1.Text = s.GetStarCount().ToString();
        }

        private void get_Click(object sender, EventArgs e)
        {
            label2.Text = DateTime.Now.Ticks.ToString();

            Random r = new Random();

            this.richTextBox1.Text = s.GetRangeData(r.Next(50, 700), r.Next(50, 700), r.Next(50, 700), 10);

            label3.Text = DateTime.Now.Ticks.ToString();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            if (s.IsSpaceInitialised())
            {
                this.get.Enabled = true;
                this.addUsersBtn.Enabled = true;
            }
        }

        private void addUsersBtn_Click(object sender, EventArgs e)
        {
            //s = new Service();
            //s.Timeout = 1000000000;

            label4.Text = "";

            Random r = new Random();
            Random r1 = new Random();

            int strlen = 0;
            string un = "";
            string pw = "password";
            char ch = 'a';

            char[] chars = new char[20];
            int j = 0;
            for (j = 0; j < 20; j++)
            {
                chars[j] = 'a';
            }

            //for (int i = 0; i < 1000; i++)
            //{
            //    do
            //    {
            //        un = "";
            //        strlen = r.Next(5, 20);
            //        for (int j = 0; j < strlen; j++)
            //        {
            //            ch = (char)(r1.Next(97, 123));
            //            un += ch;
            //        }
            //    } while (s.ContainsUserName(un));
            //    s.AddUser(un, pw);
            //}

            int count = 1000000;
            int cn = 0;
            j = 0;
            int x = 0;

            for (int i = 0; i < 20 && cn < count; i++)
            {
                //for (j = 0; j < i; j++ )
                //{
                //    chars[j] = 'a';
                //}

                j = 0;

                while (j <= i && cn < count)
                {
                    while (chars[j] < 'z' && cn < count)
                    {
                        chars[j]++;
                        un = new string(chars);
                        s.AddUser(un, pw);
                        cn++;
                    }
                    j++;
                    if (j < 20 && cn < count)
                    {
                        chars[j]++;
                        un = new string(chars);
                        s.AddUser(un, pw);
                        cn++;

                        for (x = 0; x < j; x++)
                        {
                            chars[x] = 'a';
                        }
                    }
                }
            }

            label4.Text = s.UsersCount().ToString() + " users added";
        }
    }
}